import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Index from "../views/Index";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta:{
      showNav: true
    }
  },
  {
    path: '/index',
    name: 'Index',
    component: Index,
    meta:{
      isToken: true
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//设置路由导航拦截：
router.beforeEach((to,from,next)=>{
  if(!window.localStorage.getItem('token') && !window.localStorage.getItem('username') && to.meta.isToken == true){
    router.push('/login')
    this.$message.error('请重新登录')
    return
  }
  next()
})

export default router
